﻿#include "ImageSaveManager.h"
#include <QApplication>
#include <QGroupBox>
#include <QHBoxLayout>
#include <QFileDialog>
#include <QSettings>


ImageSaveManager::ImageSaveManager(QString folder, QObject *parent)
    : QObject{parent}
{
    qRegisterMetaType<ImageSaveInfo>("ImageSaveInfo");
    qRegisterMetaType<QList<ImageSaveInfo>>("QList<ImageSaveInfo>");
    initImageSaveWorker();
    initWgtSavePath();
    ledtPath->setText(folder);
}

void ImageSaveManager::initImageSaveWorker()
{
    mSaveWorker = new ImageSaveWorker();
    mSaveWorkerThr = new QThread();
    mSaveWorker->moveToThread(mSaveWorkerThr);
    connect(mSaveWorker, &ImageSaveWorker::destroyed, mSaveWorkerThr, &QThread::quit);
    connect(mSaveWorkerThr, &QThread::finished, mSaveWorkerThr, &QObject::deleteLater);
    connect(qApp, &QApplication::aboutToQuit, mSaveWorker, &ImageSaveWorker::onQuit);
    mSaveWorkerThr->start();
}

void ImageSaveManager::initWgtSavePath()
{
    pbtnSelectFolder = new QPushButton(tr("选择存图路径"));
    ledtPath = new QLineEdit("");
    ledtPath->setEnabled(false);
    wgtSavePath = new QGroupBox(tr("存图配置"));
    QHBoxLayout* hLayout = new QHBoxLayout();
    wgtSavePath->setLayout(hLayout);
    hLayout->addWidget(pbtnSelectFolder);
    hLayout->addWidget(ledtPath);
    connect(pbtnSelectFolder, &QPushButton::clicked, this, [&](){
        QString folderName = QFileDialog::getExistingDirectory(nullptr,
                                                               "select Foler",
                                                               QApplication::applicationDirPath(),
                                                               QFileDialog::ShowDirsOnly);//打开打开文件对话框
        if(!folderName.isEmpty()){
            ledtPath->setText(folderName);
            emit sigSavePathChanged(folderName);
        }
    });

}
